﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml;
//using System.Xml.Linq;

using YouHP.Utility;
using YouHP.BLL;
using YouHP.Model;

namespace web.Pages.xml
{
    // -----------------------------------------------------------------------------------------
    // This class will provides prescription data for the "Prescription Selection Page".
    // Author: Zhi Wang
    // Date: 04/18/2009
    // -----------------------------------------------------------------------------------------
    public partial class prescriptionlist_xml : System.Web.UI.Page
    {
        #region Data

        // Current user info.
        protected UserInfo UserData = new UserInfo();
        protected PatientRecordInfo PrData;
        // Prescription data list to be output.
        protected List<PrescriptionInfo> prescriptionDataList;
        // Prescription search conditions.
        protected PrescriptionSearchCondition prescriptionSearchCond = new PrescriptionSearchCondition();

        // Paging informaton
        protected int PrescriptionPageSize = 100;
        protected int PrescriptionPageIndex = 1;

        #endregion

        #region Method

        protected void ResponseContext(int iCode, string sMsg)
        {
            // Create a xml document.
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            settings.IndentChars = ("    ");
            Response.ContentType = "text/xml";
            using (XmlWriter wr = XmlWriter.Create(Response.OutputStream, settings))
            {
                // Write XML data.
                wr.WriteStartElement("result");
                wr.WriteAttributeString("code", iCode.ToString());
                wr.WriteAttributeString("msg", sMsg);
                if (iCode == 0)
                {
                    wr.WriteStartElement("prescriptions");
                    int prescriptionCount = prescriptionDataList.Count;
                    wr.WriteAttributeString("count", prescriptionCount.ToString());

                    foreach (PrescriptionInfo info in prescriptionDataList)
                    {
                        DoctorInfo doctorInfo = DoctorBLL.Instance.GetDoctorInfo(info.DoctorId);

                        wr.WriteStartElement("prescription");
                        wr.WriteAttributeString("id", info.Id.ToString());
                        wr.WriteAttributeString("doctorid", info.DoctorId.ToString());
                        wr.WriteAttributeString("hospital", doctorInfo.Hospital);
                        wr.WriteAttributeString("doctorname", doctorInfo.Name);
                        wr.WriteAttributeString("name", info.Name.ToString());
                        wr.WriteAttributeString("averagescore", info.AverageScore.ToString());
                        wr.WriteAttributeString("diseaseid", info.DiseaseId.ToString());
                        wr.WriteAttributeString("diseasename", info.DiseaseName);
                        wr.WriteAttributeString("isclassic", info.IsClassic.ToString());
                        wr.WriteAttributeString("description", info.Description);
                        wr.WriteAttributeString("createtime", info.CreateTime.ToString());
                        wr.WriteEndElement();
                    }
                }
                wr.WriteEndElement();
                wr.WriteEndElement();
                wr.Flush();
            }
        }

        protected bool CheckLogin()
        {
            UserData.Id = CookiesTools.GetUserIdCookie();
            if (UserData.Id > 0) //表示永久登陆的用户
            {
                UserData.UserName = CookiesTools.GetUserNameCookie();   //直接从cookie中获取用户名
                if (UserData.UserName.Length <= 0)
                {
                    UserData.Id = -1;    //如果不存在用户名，则为无效的cookie
                    return false;
                }
            }
            return true;
        }

        protected bool CheckParam(ref string sMsg)
        {
            // GetFormInt or GetQueryInt ???
            // GetFormString or GetQueryString ???
            prescriptionSearchCond.PrescriptionId = RequestTools.GetFormInt("prescriptionid", -1);
            prescriptionSearchCond.PrescriptionName = RequestTools.GetFormString("prescriptionname").Trim();
            prescriptionSearchCond.DoctorId = RequestTools.GetFormInt("doctorid", -1);
            prescriptionSearchCond.DiseaseId = RequestTools.GetFormInt("diseaseid", -1);
            prescriptionSearchCond.DiseaseName = RequestTools.GetFormString("diseasename").Trim();

            int isClassic = RequestTools.GetFormInt("isclassic", -1);
            if (isClassic >= 0)
            {
                prescriptionSearchCond.UseIsClassicAsCriterion = true;
                prescriptionSearchCond.IsClassic = (isClassic > 0);
            }

            double minScore = RequestTools.GetFormDouble("minscore", -1.0);
            if (minScore >= 0.0)
            {
                prescriptionSearchCond.UseMinScoreAsCriterion = true;
                prescriptionSearchCond.MinScore = minScore;
            }

            double maxScore = RequestTools.GetFormDouble("maxscore", -1.0);
            if (maxScore >= 0.0)
            {
                prescriptionSearchCond.UseMaxScoreAsCriterion = true;
                prescriptionSearchCond.MaxScore = minScore;
            }

            if (minScore >= 0.0 && maxScore >= 0.0 && minScore >= maxScore)
            {
                sMsg = "Reversed minScore and maxScore!!";
                return false;
            }

            return true;
        }

        protected void Handle()
        {
            if (!CheckLogin())
            {
                ResponseContext(-1000, "你还没有登录");
                return;
            }

            string sErrMsg = "";
            if (!CheckParam(ref sErrMsg))
            {
                ResponseContext(-1001, sErrMsg);
                return;
            }

            PrData = PatientBLL.Instance.GetPatientRecordInfo(UserData.Id);
            if (PrData.Id <= 0)
            {
                ResponseContext(-100, "请先填写患病基本信息");
                return;
            }

            int count = 0;
            prescriptionDataList = PrescriptionBLL.Instance.GetPrescriptionList(
                PrescriptionPageSize,
                PrescriptionPageIndex,
                prescriptionSearchCond.ToSqlConditionString(),
                out count);

            ResponseContext(0, "成功");
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            Handle();
        }

        #endregion
    }
}
